﻿function createMarketOffersGrid(){
	updateYML = function(){
		Ext.Ajax.request({
			url: '/yamarket/yml/update/'
			,success: function(rsp){
				var t = eval("("+rsp.responseText+")");
			}
			,failure: function(){
				alert("Произошла ошибка");
			}
		});
	}
	delRec = function(){
		var c = grid.selModel.getCount();
		if(c < 1){
			Ext.MessageBox.alert('Ошибка','Не выделено ни одной записи.');
			return;
		}
		Ext.MessageBox.confirm('Подтверждение','Вы действительно хотите удалить выделенное?',
			function(btn){
				if (btn!='yes') return;
				offersStore.remove(grid.selModel.getSelected());
		});
	}
	var offersStore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '/yamarket/offers'}),
		restful: true,
		reader: new Ext.data.JsonReader({root: 'data',totalProperty: 'total',idProperty: 'Id'},[ 
			{name: 'Publish', type: 'boolean', mapping: 'Publish'}
			,{name: 'Id', type: 'int', mapping: 'Id'}
			,{name: 'NodeId', type: 'int', mapping: 'NodeId'}
			,{name: 'Price', type: 'float', mapping: 'Price'}
			,{name: 'CurrencyId', type: 'string', mapping: 'CurrencyId'}
			,{name: 'CategoryId', type: 'int', mapping: 'CategoryId'}
			,{name: 'TypePrefix', type: 'string', mapping: 'TypePrefix'}
			,{name: 'Vendor', type: 'string', mapping: 'Vendor'}
			,{name: 'VendorCode', type: 'string', mapping: 'VendorCode'}
			,{name: 'Model', type: 'string', mapping: 'Model'}
			,{name: 'Store', type: 'boolean', mapping: 'Store'}
			,{name: 'Pickup', type: 'boolean', mapping: 'Pickup'}
            ,{name: 'Bid', type: 'int', mapping: 'Bid'}
			,{name: 'Cid', type: 'int', mapping: 'Cid'}
			,{name: 'Delivery', type: 'boolean', mapping: 'Delivery'}
			,{name: 'Description', type: 'string', mapping: 'Description'}
			,{name: 'LocalDeliveryCost', type: 'float', mapping: 'LocalDeliveryCost'}
			,{name: 'Available', type: 'boolean', mapping: 'Available'}
			,{name: 'SalesNotes', type: 'string', mapping: 'SalesNotes'}
			,{name: 'ManufacturerWarranty', type: 'boolean', mapping: 'ManufacturerWarranty'}
			,{name: 'CountryOfOrigin', type: 'string', mapping: 'CountryOfOrigin'}
		])
		,writer:new Ext.data.JsonWriter({encode: false}),
	});
	offersStore.load();
	
	//var _initCB = {inputValue: 1, trueText: 'да', falseText: 'нет'};
		
	var grid = new Ext.grid.EditorGridPanel({
		closable: true,
		store: offersStore,
		clicksToEdit:1,
		height: 350,
		width: 600,
		title: 'Товары маркета',
		enableDragDrop   : true,
		enableDD: true,
		ddGroup: 'firstGridDDGroup',
		selModel: new Ext.grid.RowSelectionModel({singleSelect:false})
		,bbar: new Ext.PagingToolbar({
			pageSize: 25,
			store: offersStore,
			displayInfo: true,
			displayMsg: 'записи {0} - {1} из {2}',
			emptyMsg: "нет записей",
		})
		,tbar: [
			{text: 'Удалить запись',tooltip: 'Удаление выделенной записи', handler: delRec}
			,new Nippy.SearchField({ store: offersStore, params: {start: 0, limit: 25}, width: 120})
			,{text: 'Обновить YML файл',tooltip: 'Обновить YML файл', handler: updateYML}
		]
		,columns: [{header: 'Публиковать', tooltip:'Публиковать', width: 30, sortable: true, dataIndex: 'Publish', xtype: 'booleancolumn', trueText: 'да', falseText: 'нет', editor: new Ext.form.Checkbox({inputValue: 1})}
			,{header: '#', width: 30, sortable: true, dataIndex: 'Id'}
			,{header: 'Узел', tooltip:'Узел', width: 37, sortable: true, dataIndex: 'NodeId'}
            ,{header: 'Bid', tooltip:'Ставка', width: 37, sortable: true, dataIndex: 'Bid',editor: new Ext.form.NumberField({allowBlank: true,allowNegative: false})}
			,{header: 'Cid', tooltip:'Ставка для карточек модели', width: 37, sortable: true, dataIndex: 'Cid',editor: new Ext.form.NumberField({allowBlank: true,allowNegative: false})}
			,{header: 'Цена', width: 50, sortable: true, dataIndex: 'Price',editor: new Ext.form.NumberField({allowBlank: false,allowNegative: false})}
			,{header: 'Валюта', width: 50, sortable: true, dataIndex: 'CurrencyId',editor: new Ext.form.TextField({allowBlank: false,maxLength: 64})}
			,{header: 'Категория', width: 50, sortable: true, dataIndex: 'CategoryId',editor: new Ext.form.NumberField({allowBlank: false,allowNegative: false})}
			,{header: 'TypePrefix', width: 75, sortable: true, dataIndex: 'TypePrefix',editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})}
			,{header: 'Vendor', width: 75, sortable: true, dataIndex: 'Vendor',editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})}
			,{header: 'Model', width: 75, sortable: true, dataIndex: 'Model', editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})}
			,{header: 'Описание', width: 150, sortable: true, dataIndex: 'Description', editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})}
			,{header: 'Страна', width: 50, sortable: true, dataIndex: 'CountryOfOrigin', editor: new Ext.form.TextField({allowBlank: false,maxLength: 64})}
			,{header: 'На складе', width: 60, sortable: true, dataIndex: 'Store', xtype: 'booleancolumn', trueText: 'да', falseText: 'нет', editor: new Ext.form.Checkbox({inputValue: 1})}
			,{header: 'Самовывоз', width: 60, sortable: true, dataIndex: 'Pickup', xtype: 'booleancolumn', trueText: 'да', falseText: 'нет', editor: new Ext.form.Checkbox({inputValue: 1})}
			,{header: 'Доставка', width: 60, sortable: true, dataIndex: 'Delivery', xtype: 'booleancolumn', trueText: 'да', falseText: 'нет', editor: new Ext.form.Checkbox({inputValue: 1})}
			,{header: 'Цена доставки', width: 75, sortable: true, dataIndex: 'LocalDeliveryCost', editor: new Ext.form.NumberField({allowBlank: false,allowNegative: false})}
			,{header: 'В наличии', width: 60, sortable: true, dataIndex: 'Available',  xtype: 'booleancolumn', trueText: 'да', falseText: 'нет', editor: new Ext.form.Checkbox({inputValue: 1})}
			,{header: 'Заметки', width: 75, sortable: true, dataIndex: 'SalesNotes', editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})}
			,{header: 'Гарантия вендора', width: 75, sortable: true, dataIndex: 'ManufacturerWarranty',  xtype: 'booleancolumn', trueText: 'да', falseText: 'нет', editor: new Ext.form.Checkbox({inputValue: 1})}
			,{header: 'VendorCode', tooltip:'VendorCode', width: 35, sortable: true, dataIndex: 'VendorCode',editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})}
		]//column
	});
	grid.on('afterrender', function(){
		gridDropTargetEl = grid.getView().scroller.dom;
		new Ext.dd.DropTarget(grid.getView().scroller.dom, {
			ddGroup : 'siteTreeDDGroup',
			notifyDrop : function(ddSource, e, data){
				var n = ddSource.dragData.node;
				if (undefined==n) return false;
				var s = grid.getStore();
				var rt = s.recordType;
				var rec = new rt({NodeId: n.id, Description: 'Новый1 товар '+n.id+n.name});
				s.insert(0, rec);
				return true;
			}
		});
	});
	return grid;
}//function